package cn.hnucm.orderservice.controller;


import cn.hnucm.orderservice.entity.TbLog;
import cn.hnucm.orderservice.service.TbLogService;
import cn.hnucm.orderservice.utils.MyLog;
import cn.hnucm.orderservice.utils.Result;
import cn.hnucm.orderservice.utils.ResultCode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 系统日志 前端控制器
 * </p>
 *
 * @author chenjintao
 * @since 2021-12-06
 */
@Api(tags = "系统日志")
@RestController
@RequestMapping("/orderservice/tbLog")
public class TbLogController {
    @Autowired
    private TbLogService tbLogService;

    /**
     * 添加一条Log记录
     * @param tbLog
     * @return
     */
    @ApiOperation("添加一条Log记录")
    @PostMapping("/addLog")
    @ResponseBody
    @MyLog("添加一条Log记录")
    public Result addLog(@RequestBody TbLog tbLog){
        if (tbLog == null) return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        if (!tbLogService.addLog(tbLog)) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        return Result.ok();
    }

    /**
     * 通过Id删除一条Log记录
     * @param logId
     * @return
     */
    @MyLog("通过Id删除一条Log记录")
    @ApiOperation("通过Id删除一条Log记录")
    @ResponseBody
    @GetMapping("/deleteLogById/{logId}")
    public Result deleteLogById(@PathVariable("logId") Integer logId){
        if (logId == null) return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        if (!tbLogService.deleteLogById(logId)) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        return Result.ok();
    }

    /**
     * 查询所有的Log，按照page分页
     * @param pageSize
     * @param currentPage
     * @return
     */
    @MyLog("查询所有的Log，按照page分页")
    @ApiOperation("查询所有的Log，按照page分页")
    @ResponseBody
    @GetMapping("/seleteLogByPage/{pageSize}/{currentPage}")
    public Result seleteLogByPage(@PathVariable("pageSize") Integer pageSize,
                                  @PathVariable("currentPage") Integer currentPage){
        if (pageSize == null) pageSize = 10;
        if (currentPage == null) currentPage =0;
        IPage<TbLog> logs = tbLogService.seleteLogByPage(pageSize, currentPage);
        if (logs==null) return Result.error(ResultCode.SERVER_ERROR);
        else if (logs.getSize()==0) return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        return Result.ok("logs",logs);
    }

    /**
     * 查询一条Log
     * @param logId
     * @return
     */
    @MyLog("查询一条Log")
    @ApiOperation("查询一条Log")
    @GetMapping("/selectLogById/{logId}")
    @ResponseBody
    public Result selectLogById(@PathVariable Integer logId){
        if (logId == null) return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        TbLog tbLog = tbLogService.selectLogById(logId);
        if (tbLog == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        return Result.ok("log",tbLog);
    }
}

